package com.chen.demo04;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyProxyInvocation implements InvocationHandler {
    private Object target;

    public void setTarget(Object target){
        this.target = target;
    }

    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        sayHello();
        Object result = method.invoke(target, args);
        return result;
    }

    private void sayHello(){
        System.out.println("Hello");
    }
}
